package com.cdm.beans.user;

import java.util.List;

import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import net.sf.frails.JSFHelper;
import net.sf.frails.aop.annotations.InjectBean;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.InVar;
import net.sf.frails.bean.annotations.PreRender;
import net.sf.frails.bean.annotations.Prop;
import net.sf.frails.utils.FacesMessageUtils;

import com.cdm.domain.dao.ContentDAO;
import com.cdm.domain.dao.UserDAO;
import com.cdm.domain.service.Service;
import com.cdm.domain.service.ServiceImpl;
import com.cdm.persistence.Content;
import com.cdm.persistence.EntityManagerHelper;
import com.cdm.persistence.User;
import com.cdm.util.Constants;
import com.vivo.util.DTool;

@DefMbean
public class CdmBean {

	private static final String thisPage = "/user/cdm";
	@Prop
	String ifamePage="cdm_unauthcps.jsp";

	@Prop
	User loginUser;

//	@Prop
//	List<Content> contents;

	@Prop
	boolean create = true;

//	@InVar
//	@Prop
//	User user;

//	@Prop
//	List<User> unauthcps;
//	@Prop
//	List<User> unauthsps;
//
//	@Prop
//	List<User> cps;
//	@Prop
//	List<User> sps;
//
//	@Prop
//	SelectItemGroup roleList;
//
//	/*
//	 * ReadOnly Select Lists
//	 */
//	// @Prop(readOnly = true)
//	// List<User> cps;
//	public SelectItemGroup getCpItems() {
//		SelectItem[] items = new SelectItem[cps.size()];
//		for (int i = 0; i < cps.size(); i++) {
//			items[i] = new SelectItem(cps.get(i), cps.get(i).getUserName());
//		}
//		SelectItemGroup group = new SelectItemGroup();
//		group.setSelectItems(items);
//		return group;
//	}

	@SuppressWarnings("unchecked")
	@PreRender(failOutcome = thisPage)
	public boolean init() {
		Object object = JSFHelper.getFromApplication(Constants.LOGIN_USER_KEY);
		if (object instanceof User && object != null) {
			loginUser = (User) object;
		}
//		this.contents = contentDAO.findAll();
//		if (null == roleList) {
//			roleList = new SelectItemGroup();
//			roleList.setSelectItems(new SelectItem[] { new SelectItem(0, FacesMessageUtils.getInstance().getMessage("label_user_role_cp")),
//			        new SelectItem(1, FacesMessageUtils.getInstance().getMessage("label_user_role_sp")),
//			        new SelectItem(2, FacesMessageUtils.getInstance().getMessage("label_user_role_cdm")),
//			        new SelectItem(3, FacesMessageUtils.getInstance().getMessage("label_user_role_admin")),
//			        new SelectItem(4, FacesMessageUtils.getInstance().getMessage("label_user_role_enduser")) });
//		}
//
//		this.unauthcps = EntityManagerHelper.getEntityManager().createQuery(
//		        "select user from User user where " + UserDAO.AUTH_STATUS + "=? and " + UserDAO.USER_ROLE + "=?").setParameter(1,
//		        User.UNAUTH).setParameter(2, User.ROLE_CP).getResultList();
//
//		this.unauthsps = EntityManagerHelper.getEntityManager().createQuery(
//		        "select user from User user where " + UserDAO.AUTH_STATUS + "=? and " + UserDAO.USER_ROLE + "=?").setParameter(1,
//		        User.UNAUTH).setParameter(2, User.ROLE_SP).getResultList();
//
//		this.cps = service.getAuthedUsersByRole(User.ROLE_CP);
//		this.sps = service.getAuthedUsersByRole(User.ROLE_SP);

		DTool.d.debug("Enter Admins Home Page");
		return true;
	}

//	@InjectBean
//	public String auth() {
//		service.auth(user);
//		// TODO 自动取证书
//		create = true;
//		return thisPage;
//	}

	public String ifm(String ifpage){
		this.ifamePage = ifpage;
		return thisPage;
	}
//	private ContentDAO contentDAO = new ContentDAO();
//	private UserDAO userDAO = new UserDAO();
//	private Service service = new ServiceImpl();
}
